﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LengthUnit_Scaler
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        bool err_flag = true;
        bool IsFirst = true;
        int index = 0;
        int i = 0;
        public double[] temp = new double[10];
        public string[] temp1 = new string[10];
        public string[] temp2 = new string[10];
        public double[] temp3 = new double[10];

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            Thread Update = new Thread(Thread_Update);
            Update.Start();
            Update.IsBackground = true;
        }
         

        private void Thread_Update()
        {
            temp[0] = 1;
            temp[1] = 0.1;
            temp[2] = 0.001;
            temp[3] = 39.3701;
            temp[4] = 0.0393701;
            temp[5] = 0.00328084;


            while (true)
            {
                System.Threading.Thread.Sleep(1000);
                this.Dispatcher.Invoke(
                    new Action(
                        delegate
                        {

                            //if (!Regex.IsMatch(mm.Text.ToString(), @"^-?[1-9]\d*$|^0$"))
                            //{
                            //    //MessageBox.Show("请输入数字");
                            //    //mm.SelectAll();

                            //    return;
                            //}

                            if (IsFirst == true)
                            {
                                IsFirst = false;
                                temp2[0] = mm.Text;
                                temp2[1] = cm.Text;
                                temp2[2] = m.Text;
                                temp2[3] = mil.Text;
                                temp2[4] = inch.Text;
                                temp2[5] = ft.Text;
                            }

                            if (mm.SelectionStart == 0 && mm.SelectionLength == 0)
                                mm.SelectionStart = mm.Text.Length;
                            if (cm.SelectionStart == 0 && cm.SelectionLength == 0)
                                cm.SelectionStart = cm.Text.Length;
                            if (m.SelectionStart == 0 && m.SelectionLength == 0)
                                m.SelectionStart = m.Text.Length;
                            if (mil.SelectionStart == 0 && mil.SelectionLength == 0)
                                mil.SelectionStart = mil.Text.Length;
                            if (inch.SelectionStart == 0 && inch.SelectionLength == 0)
                                inch.SelectionStart = inch.Text.Length;
                            if (ft.SelectionStart == 0 && ft.SelectionLength == 0)
                                ft.SelectionStart = ft.Text.Length;

                            temp1[0] = mm.Text;
                            temp1[1] = cm.Text;
                            temp1[2] = m.Text;
                            temp1[3] = mil.Text;
                            temp1[4] = inch.Text;
                            temp1[5] = ft.Text;
                            index = -1;
                            for (int i = 0; i < 6; i++) //第一个不一样
                                if (temp1[i] != temp2[i])
                                    index = i;
                            if (index != -1) //发生了改变
                            {
                                //判断输入是否合法 
                                try
                                {
                                    temp3[index] = double.Parse(temp1[index]);
                                    err_flag = true;
                                }
                                catch (Exception ex)
                                {
                                    err_flag = false;
                                }

                                if (err_flag == false) //如果不合法
                                {
                                    mm.Text = "";
                                    cm.Text = "";
                                    m.Text = "";
                                    mil.Text = "";
                                    inch.Text = "";
                                    ft.Text = "";
                                    switch (index)
                                    {
                                        case 0: mm.Text = temp1[index]; mm.SelectionStart = mm.Text.Length; break;
                                        case 1: cm.Text = temp1[index]; cm.SelectionStart = cm.Text.Length; break;
                                        case 2: m.Text = temp1[index]; m.SelectionStart = m.Text.Length; break;
                                        case 3: mil.Text = temp1[index]; mil.SelectionStart = mil.Text.Length; break;
                                        case 4: inch.Text = temp1[index]; inch.SelectionStart = inch.Text.Length; break;
                                        case 5: ft.Text = temp1[index]; ft.SelectionStart = ft.Text.Length; break;
                                        default: break;
                                    }
                                }
                                else // //如果合法
                                {
                                    temp3[0] = double.Parse(temp1[index]) / temp[index];
                                    for ( i = 1; i < 6; i++)
                                        temp3[i] = temp3[0] * temp[i];
                                     

                                    mm.Text = temp3[0].ToString();
                                    cm.Text = temp3[1].ToString();
                                    m.Text = temp3[2].ToString();
                                    mil.Text = temp3[3].ToString();
                                    inch.Text = temp3[4].ToString();
                                    ft.Text = temp3[5].ToString();

                                }

                            }

                            temp2[0] = mm.Text;
                            temp2[1] = cm.Text;
                            temp2[2] = m.Text;
                            temp2[3] = mil.Text;
                            temp2[4] = inch.Text;
                            temp2[5] = ft.Text;
                        }
                    )
                );
        }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }
    }
}
